package com.dy.栈.顺序栈;

import com.dy.栈.IStack;

public class SqStack implements IStack {
    private Object[] stackElem;
    private int top;//指向栈顶元素的下一个位置，空栈top=0

    public SqStack(int maxSize){
        top =0;
        stackElem = new Object[maxSize];
    }


    @Override
    public void clear() {
        top=0;
    }

    @Override
    public boolean isEmpty() {
        return top==0;
    }

    @Override
    public int length() {
        return top;
    }

    @Override
    public Object peek() {
        if(!isEmpty()){
            return stackElem[top-1];
        }
        else
            return null;
    }

    @Override
    public void push(Object x) throws Exception {
        if(top==stackElem.length){
            throw new Exception("栈已满");
        }
        else
            stackElem[top++] = x;
    }

    @Override
    public Object pop() {
        if(isEmpty()){
            return null;
        }
        else
            return stackElem[--top];
    }

    public void display(){
        for(int i=top-1;i>=0;i--){
            System.out.println(stackElem[i].toString()+" ");
        }
    }
}
